package com.io.buff;

import org.junit.Test;

import java.io.*;

public class BufferedInputStream_ {
    public static void main(String[] args) {
            System.out.println("使用BufferedInputStream 和 BufferedOutputStream 拷贝文件");
    }

    @Test
    public void copy() {
        // 操作二进制文件
        //String filePath = "E:\\kaola.jpeg";
        //String target = "D:\\kaola.jpeg";
        // 视频二进制
        //String filePath = "E:\\jue.mp4";
        //String target = "D:\\juezi.mp4";
        // 最小单位是字节
        // 可以操作文本
        String filePath = "E:\\a.java";
        String target = "D:\\a.java";
        BufferedInputStream  bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(filePath));
            bos = new BufferedOutputStream(new FileOutputStream(target));
            // 读取长度
            int readLen = 0;
            // 读取内容数组
            byte[] buf = new byte[1024];
            // 拷贝文件 本质 边读边写
            // 当返回 -1 表示读取内容结束
            while ((readLen = bis.read(buf)) != -1) {
                // 输出读取的内容
                System.out.println(new String(buf,0,readLen));
                // 写入
                bos.write(buf,0,readLen);
            }
            System.out.println("文件拷贝成功~~");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源流
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void read() {
        System.out.println("使用");
        // 读取文件
        String path = "E:\\test\\test.txt";
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(path));
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = bis.read(buf)) != -1) {
                // 输出内容
                System.out.println(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭外层资源流，底层会自动关闭节点流
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void writer() {
        // 写文件内容
        String path = "E:\\test\\test.txt";
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(path, true));
            // 写入内容
            String str = "\r\n不负青春，不负韶华!\r\n";
            bos.write(str.getBytes());
            String note = "《以梦为马》";
            bos.write(note.getBytes());
            System.out.println("写入结束");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭外层资源流，底层自动关闭节点流
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
